//
//  RootViewController.m
//  iStage
//
//  Created by michael on 24/07/2008.
//  Copyright Legal Education Technology Lab 2008. All rights reserved.
//

#import "RootViewController.h"
#import "iStageAppDelegate.h"
#import "ProductionViewController.h"
#import "DataManager.h"

@implementation RootViewController

@synthesize pvc;

- (void) init{
	[super init];
	self.title =@"iStage";
	self.pvc = [[ProductionViewController alloc] initWithNibName:@"ProductionViewController" bundle:nil];	
}
-(void)newProductionAction {
	[[DataManager sharedDataManager] addNewProductionWithTitle:@"New Production"];
/*
	NSInteger lastProductionIndex = [[DataManager sharedDataManager].productionList count]-1;
	[[DataManager sharedDataManager] setCurrentProduction:lastProductionIndex];
	[[DataManager sharedDataManager].currentProduction setObject:[NSDate date] forKey:@"startdate"];
	[[DataManager sharedDataManager].currentProduction setObject:[NSDate date] forKey:@"enddate"];
 */
	[self.tableView reloadData];
//	[self displayProductionAtIndex:lastProductionIndex];
}
-(void)editProductionListAction {

	self.navigationItem.rightBarButtonItem= nil;
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(endEditProductionListAction)];
	[self setEditing:YES animated:YES];
}
-(void)endEditProductionListAction {

	self.navigationItem.rightBarButtonItem = 		
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
												  target:self action:@selector(newProductionAction)];
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editProductionListAction)];
	[self setEditing:NO animated:YES];
}
- (void)viewDidLoad {
	// Add the following line if you want the list to be editable
	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
	self.navigationItem.rightBarButtonItem = 		
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
												  target:self action:@selector(newProductionAction)];
	
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editProductionListAction)];
	
	[DataManager readFromDisk];
	
	[self.tableView reloadData];
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
	if (section==0) {
		return @"Productions";
	}
	return @"Tools";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section ==0) {
	return [[[DataManager sharedDataManager] productionList] count];
	}
	return 3; //for the other tools
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell
	if (indexPath.section==0) {
		DataManager *dm =[DataManager sharedDataManager];

		cell.text = [[dm.productionList objectAtIndex:indexPath.row] valueForKey:@"productionname"];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	if (indexPath.section==1) {
		if (indexPath.row==0) {
		cell.text=@"Save";
		}
		if (indexPath.row ==1) {
			cell.text=@"Load";
		}
		if (indexPath.row==2) {
			cell.text=@"Re-init";
		}
	}
	return cell;
		
}
-(void)displayProductionAtIndex:(NSInteger)index {
	[[DataManager sharedDataManager] setCurrentProduction:index];
	
	NSLog(@"Displaying Production View Controller");
	if (pvc == nil) {
		pvc =[[ProductionViewController alloc] initWithNibName:@"ProductionViewController" bundle:nil];
		
	}
	[pvc.tableView reloadData];
	[self.navigationController pushViewController:pvc animated:YES];	
}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	 // Navigation logic
	 if (indexPath.section==0) {
	 if (self.editing) {
		 
	 }
	 else {
		 [self displayProductionAtIndex:indexPath.row];
	 }
	 }
	 if (indexPath.section==1) {
		 if (indexPath.row == 0) {
			 [DataManager persistToDisk];
		 }
		 if (indexPath.row==1) {
			 [DataManager readFromDisk]; 
			 [self.tableView reloadData];

		 }
		 if (indexPath.row==2) {
			 [[DataManager sharedDataManager] initialise];
			 [self.tableView reloadData];
		 }
 			 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
	 }
}


/*
 Override if you support editing the list
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
		
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		// Delete the row from the data source
		NSLog(@"Removing production from data");
		[[DataManager sharedDataManager].productionList removeObjectAtIndex:indexPath.row];
		NSLog(@"Removing row from tableview");
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
		NSLog(@"Reloading Data");
		[tableView reloadData];
	}	
	if (editingStyle == UITableViewCellEditingStyleInsert) {
		// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
	}	
}


/*
 Override if you support conditional editing of the list
 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the specified item to be editable.
	if (indexPath.section==1) {
		return NO;
	}
	return YES;
}



/*
 Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
 Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the item to be re-orderable.
	return YES;
}
 */ 


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.tableView	reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return YES;
	//	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{


}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[pvc release];
	[super dealloc];
}
-(IBAction)refreshData {
	[self.tableView reloadData];
}

@end

